package homework;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author DCSGO
 * @version 1.0
 * 1.利用Class类的forName方法得到File类的class对象
 * 2.在控制台打印File类的所有构造器
 * 3.通过newInstance的方法创建File对象，并创建myNew.txt文件
 */
public class Homework02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        Class<?> fileCls = Class.forName("java.io.File");
        Constructor<?>[] declaredConstructors = fileCls.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
        Constructor<?> constructor = fileCls.getConstructor(String.class);
        File file = (File) constructor.newInstance("src\\homework\\myNew.txt");
        if (file.createNewFile()) {
            System.out.println("文件创建成功！");
        }
        /*
         * public java.io.File(java.lang.String,java.lang.String)
         * public java.io.File(java.lang.String)
         * private java.io.File(java.lang.String,java.io.File)
         * public java.io.File(java.io.File,java.lang.String)
         * public java.io.File(java.net.URI)
         * private java.io.File(java.lang.String,int)
         * 文件创建成功！
         *
         * 进程已结束，退出代码为 0
         */
    }
}
